﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class cms_default : System.Web.UI.Page
{

    PlanningManager pm;
    DateTimeConverter dtc = new DateTimeConverter();

    protected void Page_Load(object sender, EventArgs e)
    {

        pm = new PlanningManager((debioscoopEntities)Session["db"]);

        if (!IsPostBack)
        {


            PlanningGridView.Sort("zaalzaalnummer ASC, datum DESC, tijd ASC", SortDirection.Ascending);

        }
        
    }

    //protected void PlanningGridView_SelectedIndexChanged(object sender, EventArgs e)
    //{
    //    //PlanningDetailsOverview.ChangeMode(DetailsViewMode.ReadOnly);
    //    //string select_id = PlanningGridView.DataKeys[PlanningGridView.SelectedIndex]["planning_id"].ToString();
    //    //Planning_Select.SelectParameters.Clear();
    //    //Planning_Select.SelectParameters.Add("planning_id", select_id);
    //    //PlanningDetailsOverview.DataBind();
    //}
    protected void PlanningDetailsOverview_ItemInserting(object sender, DetailsViewInsertEventArgs e)
    {
        try
        {

            DropDownList FilmsList = (DropDownList)PlanningDetailsOverview.FindControl("Film_drop");
            DropDownList BiosList = (DropDownList)PlanningDetailsOverview.FindControl("Bioscoop_drop");
            DropDownList ZaalList = (DropDownList)PlanningDetailsOverview.FindControl("Zaal_drop");
            CheckBox Drie_D = (CheckBox)PlanningDetailsOverview.FindControl("Drie_D");
            TextBox time = (TextBox)PlanningDetailsOverview.FindControl("txtTijd");
            TextBox date = (TextBox)PlanningDetailsOverview.FindControl("txtDatum");
            
            planning insertPlanning = new planning();
            insertPlanning.zaalbioscoopbioscoop_id = int.Parse(BiosList.SelectedValue);
            insertPlanning.tijd = TimeSpan.Parse(time.Text);
            insertPlanning.datum = DateTime.Parse(date.Text);
            insertPlanning.dried = Drie_D.Checked;



            //not sure if I should use this. I didn't need it elsewhere
            insertPlanning.zaalzaalnummer = int.Parse(ZaalList.SelectedValue);
            insertPlanning.filmfilm_id = int.Parse(FilmsList.SelectedValue);


            pm.addPlanning(insertPlanning);
        }
        catch (System.Data.Entity.Validation.DbEntityValidationException dbEx)
        {
            foreach (var validationErrors in dbEx.EntityValidationErrors)
            {
                foreach (var validationError in validationErrors.ValidationErrors)
                {
                    System.Diagnostics.Trace.TraceInformation("Property: {0} Error: {1}", validationError.PropertyName, validationError.ErrorMessage);
                }
            }
        }
    }
    protected void PlanningDetailsOverview_ItemUpdating(object sender, DetailsViewUpdateEventArgs e)
    {
        DropDownList FilmsList = (DropDownList)PlanningDetailsOverview.FindControl("Film_drop");
        DropDownList BiosList = (DropDownList)PlanningDetailsOverview.FindControl("Bioscoop_drop");
        DropDownList ZaalList = (DropDownList)PlanningDetailsOverview.FindControl("Zaal_drop");
        CheckBox Drie_D = (CheckBox)PlanningDetailsOverview.FindControl("Drie_D");
        TextBox time = (TextBox)PlanningDetailsOverview.FindControl("txtTijd");
        TextBox date = (TextBox)PlanningDetailsOverview.FindControl("txtDatum");

        //PlanningDetails.InsertCommand = "UPDATE planning SET tijd=" + time.Text + ", datum=" + date.Text + ", dried=" + Drie_D + ", zaalbioscoopbioscoop_id=" + BiosList.SelectedValue + ", zaalzaalnummer=" + ZaalList.SelectedValue + ", filmfilm_id=" + FilmsList.SelectedValue + " WHERE planning_id =" + PlanningGridView.SelectedValue;
        //PlanningDetails.Insert();


        planning updatePlanning = pm.getPlanning(int.Parse(PlanningGridView.SelectedValue.ToString()));
        updatePlanning.zaalbioscoopbioscoop_id = int.Parse(BiosList.SelectedValue);
        updatePlanning.zaalzaalnummer = int.Parse(ZaalList.SelectedValue);
        updatePlanning.filmfilm_id = int.Parse(FilmsList.SelectedValue);
        updatePlanning.tijd = TimeSpan.Parse(time.Text);
        updatePlanning.datum = DateTime.Parse(date.Text);
        updatePlanning.dried = Drie_D.Checked;
        pm.executeQuery();

        
    }
    protected void PlanningDetailsOverview_ItemDeleting(object sender, DetailsViewDeleteEventArgs e)
    {
     
    }

    protected void Zaal_drop_DataBound(object sender, EventArgs e)
    {
        DropDownList ZaalList = (DropDownList)PlanningDetailsOverview.FindControl("Zaal_drop");
        Label lblZaal = (Label)PlanningDetailsOverview.FindControl("lblZaalPlan");

        ZaalList.SelectedValue = lblZaal.Text;
    }
    protected void Bioscoop_drop_DataBound(object sender, EventArgs e)
    {
        DropDownList BioscoopList = (DropDownList)PlanningDetailsOverview.FindControl("Bioscoop_drop");
        Label lblBioscoop = (Label)PlanningDetailsOverview.FindControl("lblBiosPlan");

        BioscoopList.SelectedValue = lblBioscoop.Text;

    }
    protected void Film_drop_DataBound(object sender, EventArgs e)
    {
        DropDownList FilmList = (DropDownList)PlanningDetailsOverview.FindControl("Film_drop");
        Label lblFilm = (Label)PlanningDetailsOverview.FindControl("lblFilmPlan");

        FilmList.SelectedValue = lblFilm.Text;
    }
    protected void PlanningDetailsOverview_ItemInserted(object sender, DetailsViewInsertedEventArgs e)
    {
       // PlanningGridView.DataBind();
        Response.Redirect(Request.RawUrl);
    }
    protected void PlanningDetailsOverview_ItemUpdated(object sender, DetailsViewUpdatedEventArgs e)
    {
        PlanningGridView.DataBind();
        //Response.Redirect(Request.RawUrl);
    }
    protected void PlanningDetailsOverview_ItemDeleted(object sender, DetailsViewDeletedEventArgs e)
    {
        PlanningGridView.DataBind();
       // Response.Redirect(Request.RawUrl);
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        PlanningDetailsOverview.Visible = true;
        PlanningDetailsOverview.ChangeMode(DetailsViewMode.Insert);
    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        PlanningDetailsOverview.ChangeMode(DetailsViewMode.ReadOnly);
        PlanningGridView.SelectedIndex = -1;
    }

    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        Bind();
    }

    public void Bind()
    {
        string select_id = DropDownList1.SelectedValue;
        Planning_Select.SelectParameters.Clear();
        Planning_Select.SelectParameters.Add("zaalnummer", select_id);
        PlanningGridView.DataBind();
    }

    protected void Planning_SelectedIndexChanged(object sender, EventArgs e)
    {
        PlanningDetailsOverview.ChangeMode(DetailsViewMode.ReadOnly);
    }
}